<?php
/**
 * mahuohuo 本地的计数器仿真类
 * @see https://gitee.com/foglede
 * @copyright https://gitee.com/foglede
 * @author https://gitee.com/foglede
 * 
 * 模型
 * 一个KEY，value = 一维数组的JSON
 * 2
 */
class SaeCounter{
    private $allcounter = array();
    const countername = 'counterfilename';

    private static $redis = null;
    public function __construct() {
     //$this->allcounter = $this->allcounter + (array)json_decode(wincache_file_get_contents(constant(self::countername)),TRUE);
            
    if(! (self::$redis instanceof Redis) ){ // && is_callable(self::$redis)
        self::$redis = new Redis();
        self::$redis->select(2);
    }
    self::$redis->connect('127.0.0.1', 6379);
     $this->allcounter += (array)json_decode(self::$redis->get(constant(self::countername)),TRUE);
    }
    
//    private function __autoload(){
//        $this->allcounter = $this->allcounter + (array)json_decode(wincache_file_get_contents(constant(self::counterfilename)),TRUE);
//        //$this->allcounter = !empty($this->allcounter) ? $this->allcounter : array();
//    }

    private function save($data){
        return self::$redis->set(constant(self::countername),  json_encode($data));
    }

    public function create($name,$value = 0){
        if($this->exists($name)){ return FALSE; }
        $value = sprintf('%.0f', $value);//兼容64位
        $name = array($name=>$value);
        $this->allcounter = $this->allcounter + $name;
        return $this->save($this->allcounter);
    }
    public function decr($name,$value = 1) {
        if(!$this->exists($name)){ return FALSE; }
        $this->allcounter[$name] = bcsub( $this->allcounter[$name], $value );
        $this->save($this->allcounter);
        return $this->get($name);
    }
    
    public function exists($name) {
        return array_key_exists($name,$this->allcounter);
    }
        
    public function get($name){
        if(empty ($this->allcounter)){ return FALSE; }
        //$this->__autoload();
        return $this->allcounter[$name];
    }
    public function getall(){
        return $this->allcounter;
    }
    public function incr($name,$value = 1){
        if(!$this->exists($name)){ return FALSE; }
        $this->allcounter[$name] = bcadd( $this->allcounter[$name], $value );
        //var_dump($this->allcounter);
        $this->save($this->allcounter);
        return $this->get($name);
    }
    public function length(){
        return count($this->allcounter);
    }
    public function mget($names = array()){//换个函数应该也可以
        return array_intersect_ukey($this->allcounter, $names,  function($key1,$key2){
            if (strcasecmp($key1, $key2)){return $key1;}//键区分大小写
        });
    }
    public function remove($name){
        if(!$this->exists($name)){ return FALSE; }
        unset ($this->allcounter[$name]);
        return $this->save($this->allcounter);
    }
    public function set(){
        if(func_num_args() != 2){ return FALSE; }
        $this->allcounter[func_get_arg(0)] = func_get_arg(1);
        return $this->save($this->allcounter);
    }
}

?>
